package com.example.qt.ptplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qt.ptplatform.entity.FavoriteTorrent;
import com.example.qt.ptplatform.service.FavoriteTorrentService;
import com.example.qt.ptplatform.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "favorite")
public class FavoriteTorrentController {

    @Autowired
    private FavoriteTorrentService favoriteTorrentService;

    // 检查收藏状态的主要方法
    @GetMapping("/check")
    public JsonResult checkFavoriteStatus(
            @RequestParam("user_id") Integer userId,
            @RequestParam("torrent_id") Integer torrentId) {
        try {
            // 参数校验
            if (userId == null || torrentId == null) {
                return JsonResult.error("用户ID和种子ID不能为空");
            }

            // 调用服务层检查收藏状态
            boolean isFavorite = favoriteTorrentService.isFavorite(userId, torrentId);

            // 构造返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("is_favorite", isFavorite);
            data.put("user_id", userId);
            data.put("torrent_id", torrentId);

            return JsonResult.success("查询成功", data);
        } catch (Exception e) {
            return JsonResult.error("查询收藏状态失败: " + e.getMessage());
        }
    }



    @PostMapping("/addFavorite")
    public JsonResult addFavorite(@RequestParam("user_id") int userId,
                                  @RequestParam("torrent_id") int torrentId) {
        try {
            // 检查是否已经收藏
            boolean isFavorite = favoriteTorrentService.isFavorite(userId, torrentId);
            if (isFavorite) {
                return JsonResult.error("该种子已收藏");
            }

            // 创建新的收藏记录
            FavoriteTorrent favorite = new FavoriteTorrent();
            favorite.setUserId(userId);
            favorite.setTorrentId(torrentId);
            favorite.setCreateTime(LocalDateTime.now());

            // 保存收藏记录
            boolean result = favoriteTorrentService.save(favorite);

            if (result) {
                Map<String, Object> data = new HashMap<>();
                data.put("user_id", userId);
                data.put("torrent_id", torrentId);
                return JsonResult.success("收藏成功", data);
            } else {
                return JsonResult.error("收藏失败");
            }
        } catch (Exception e) {
            return JsonResult.error("收藏失败: " + e.getMessage());
        }
    }

    @PostMapping("/removeFavorite")
    public JsonResult removeFavorite(@RequestParam("user_id") int userId,
                                     @RequestParam("torrent_id") int torrentId) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<FavoriteTorrent> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(FavoriteTorrent::getUserId, userId)
                    .eq(FavoriteTorrent::getTorrentId, torrentId);

            // 删除收藏记录
            boolean result = favoriteTorrentService.remove(wrapper);

            if (result) {
                Map<String, Object> data = new HashMap<>();
                data.put("user_id", userId);
                data.put("torrent_id", torrentId);
                return JsonResult.success("取消收藏成功", data);
            } else {
                return JsonResult.error("取消收藏失败，可能未收藏过");
            }
        } catch (Exception e) {
            return JsonResult.error("取消收藏失败: " + e.getMessage());
        }
    }

    @GetMapping("/userFavorites")
    public JsonResult getUserFavorites(@RequestParam("user_id") Integer userId) {
        try {
            // 参数校验
            if (userId == null) {
                return JsonResult.error("用户ID不能为空");
            }

            // 调用服务层获取收藏的种子ID列表
            List<Integer> torrentIds = favoriteTorrentService.getUserFavoriteTorrentIds(userId);

            // 构造返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("user_id", userId);
            data.put("torrent_ids", torrentIds);
            data.put("count", torrentIds.size());

            return JsonResult.success("获取收藏夹成功", data);
        } catch (Exception e) {
            return JsonResult.error("获取收藏夹失败: " + e.getMessage());
        }
    }
}